app.controller("z-closed-c", [ '$scope','$rootScope','$z-http','$sce','z-closed-s','z-attendance-s','z-edit-menu-s','$filter',
		function($scope,$rootScope,$zhttp,$sce,Service,AttendanceService,MenuService,$filter) {
		$scope.viewModel = {};
		$scope.viewModel.perPage = "10";
		$scope.viewModel.rowCount = "1";	
		$scope.viewModel.cdYearData=[];
		
		$scope.editModel = {};
		$scope.editModel.title="";
		$scope.editModel.closed={};
		$scope.editModel.dayList=[];
		$scope.editModel.yearData=[];
		$scope.editModel.checkAll=false;
		
		var newYear = new Date().getFullYear();
		for(var year = newYear-3;year<=newYear+3;year++ ){
			$scope.editModel.yearData.push(year);
		}
		
		for(var year = newYear-3;year<=newYear+3;year++ ){
			$scope.viewModel.cdYearData.push(year);
		}
		
		//查询页面名称
		MenuService.getMenuByUrl({url:'closed'},
		function(data){	
			$scope.viewModel.menuList=data;
		},function(error) {});
		
		//加载新增页月份数据
		$scope.editModel.bindSelectData = function(){
			$scope.editModel.closeMonthData = $scope.editModel.canUseMonth;
		};
		
		//加载数据
		$scope.viewModel.bindData = function(){
			$scope.viewModel.isLoading=true;
			//获取基本信息
			Service.getClosedPageList({
				closedYear:$scope.viewModel.closedYear,
				pageNo:$scope.viewModel.rowCount,
				pageCount:$scope.viewModel.perPage
			},
			function(data){
				$scope.viewModel.dataList = data.closedBeanList;
				$scope.viewModel.totalData = data.pageTotal;
				$scope.viewModel.isLoading=false;
			},function(error) {
				$scope.viewModel.isLoading=false;
			});
		};
		
		$scope.viewModel.bindData();
		
		//加载
		$scope.viewModel.load=function(){
			$scope.viewModel.show=true;
		};
		
		//关闭加载
		$scope.viewModel.unLoad=function(){
			$scope.viewModel.show=false;
		};

		//保存
		$scope.viewModel.add=function(){
			$scope.editModel.title="新增封帐";
			$scope.editModel.type = true;
			$scope.editModel.dayList=[];
			$scope.editModel.closed={};
			$scope.editModel.closed.closedYear = newYear+"";
			$scope.editModel.checkAll=false;
			$scope.viewModel.load();
			$scope.editForm.closeMonth.$dirty=false;
		};
		
		//保存
		$scope.viewModel.update=function(closed){
			$scope.viewModel.isLoading=true;
			//获取基本信息
			Service.getClosedById({
				closedId:closed.closedId,
			},
			function(data){
				$scope.editModel.title="修改封帐";
				$scope.editModel.closed=data;
				$scope.editModel.selectCloseMonth();
				$scope.editModel.JudgeCheckboxAll();
				$scope.editModel.type = false;
				$scope.viewModel.load();
				$scope.viewModel.isLoading=false;
			},function(error) {
				$scope.viewModel.isLoading=false;
			});
		};
		
		//验证
		$scope.viewModel.validForm= function() {
            if (!$scope.editForm.$valid) {
            	if(!$scope.editForm.closeMonth.$dirty){
            		$scope.editForm.closeMonth.$dirty=true;
            	}
                return true;
            }else{
            	var type=true;
            	// 判断封账日期是否选择
            	for(var i=0;i<$scope.editModel.dayList.length;i++){
            		if($scope.editModel.dayList[i].checkbox){
            			type=false;
            			break;
            		}
            	}
            	if(type){
            		$rootScope.alertWarning("请选择封账日期！");
            		return true;
        		}
            	return false;
            }
        };
		
		//保存
		$scope.viewModel.save = function(){
			if($scope.viewModel.validForm()){
				return;
			}
			// 赋值封账日期
			$scope.editModel.closed.closedDayList=angular.copy($scope.editModel.dayList);
			$scope.viewModel.isLoading = true;
			if($scope.editModel.type){
				Service.saveClosed($scope.editModel.closed,
						function(data){
							$scope.viewModel.unLoad();
							$rootScope.alertInfo("新增成功。");
							$scope.viewModel.bindData();
							$scope.viewModel.isLoading = false;
						},function(error) {
							$rootScope.alertError(error.fault.message);
							$scope.viewModel.isLoading = false;
						});
			}else{
				Service.updateClosed($scope.editModel.closed,
						function(data){
							$scope.viewModel.unLoad();
							$rootScope.alertInfo("修改成功。");
							$scope.viewModel.bindData();
							$scope.viewModel.isLoading = false;
						},function(error) {
							$rootScope.alertError(error.fault.message);
							$scope.viewModel.isLoading = false;
						});
			}
		};
		
		//删除
		$scope.viewModel.delele=function(closed){
			$rootScope.alertConfirm("确定删除吗？",function() {
				$scope.viewModel.isLoading=true;
				Service.deleteClosed({
					closedId:closed.closedId
				},function(){
					$scope.viewModel.bindData();
					$scope.viewModel.isLoading=false;
					$rootScope.alertInfo("删除成功。");
				},function(error){
					$scope.viewModel.isLoading=false;
					$rootScope.alertError(error.fault.message);
				});
			});
		};
		
		// 选择月份
		$scope.editModel.selectCloseMonth=function(){
			$scope.editModel.dayList=[];
			var day = new Date($scope.editModel.closed.closedYear, $scope.editModel.closed.closedMonth, 0);
			var dayCount = new Date(day.getFullYear(), (day.getMonth()+1), 0).getDate();
			//循环设置周
			for(var i=0;i<dayCount;i++){
				var mDay=new Date($scope.editModel.closed.closedYear,$scope.editModel.closed.closedMonth - 1,i+1);
				var checkbox=false;
				if($scope.editModel.closed.closedDayList!=null){
					for(var j=0;j<$scope.editModel.closed.closedDayList.length;j++){
						var closedDayDate=new Date($scope.editModel.closed.closedDayList[j].closedDayDate);
						if(closedDayDate.getTime()==mDay.getTime()){
							checkbox=true;
							break;
						}
					}
				}
				var data={};
				data.checkbox=checkbox;
				data.closedDayDate=mDay;
				$scope.editModel.dayList.push(data);
			}
		};
		
		//全选事件
		$scope.editModel.checkboxAll=function(){
			//判断是否为全选
			 if($scope.editModel.checkAll==false){
				 angular.forEach($scope.editModel.dayList, function(value, key) {
					 value.checkbox=true;
				 });
			 }else{
				 angular.forEach($scope.editModel.dayList, function(value, key) {
					 value.checkbox=false;
				 });
			 }
			 
		};
		
		//单选事件
		$scope.editModel.checkbox=function(closedDay){
			 if(closedDay.checkbox){
				 closedDay.checkbox=false;
			 }else{
				 closedDay.checkbox=true;
			 }
			 $scope.editModel.JudgeCheckboxAll();
		};
		
		//判断是否为全选
		$scope.editModel.JudgeCheckboxAll=function(){
			 var type=false;
			 for(var i=0;i<$scope.editModel.dayList.length;i++){
				 if($scope.editModel.dayList[i].checkbox==false){
					 type=false;
					 break;
				 }else{
					 type=true;
				 }
			 }
			 if(type){
				 $scope.editModel.checkAll=true;
			 }else{
				 $scope.editModel.checkAll=false;
			 }
		};
		
		// 获取周几
		$scope.editModel.closedDayDate = function(date){
			var dt=new Date(date);
			var day = ["周天", "周一", "周二", "周三", "周四", "周五", "周六"];
			return day[dt.getDay()];
		};
} ]);